<?php
class pembeliancontroller extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->model('mastersupplier_model');
		$this->load->model('masterbarangdetail_model');
		$this->load->model('pembelian_model');
		$this->load->library('session');
		$this->load->library('pagination_editable');
		if(($this->session->userdata('fullname') == '') || ($this->session->userdata('fullname') == NULL)){
			redirect('logincontroller');
		}
	}
	
	function index()
	{
		$config = array();
		$config['per_page'] = 10;
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		$config['curr_class'] = 'active';
		$config['full_tag_open'] = '<ul id="pagination-flickr">';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['next_link'] = 'Next &gt;&gt;';
		$config['prev_link'] = '&lt;&lt; Previous';
		$config['next_tag_open'] = '<li class="next">';
		$config['next_tag_close'] = '</li>';
		$config['prev_tag_open'] = '<li class="previous-off">';
		$config['prev_tag_close'] = '</li>';
		
		$config["uri_segment"] = 3;
		$config["base_url"] = site_url('pembeliancontroller/index');
		$config['total_rows'] = $this->pembelian_model->countGetList();
		
		$this->pagination_editable->initialize($config);
	
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		               
		if (isset($_POST['btnview'])) {
            if ((isset($_POST['tglawal']) && isset($_POST['tglakhir'])) && (!empty($_POST['tglawal']) && !empty($_POST['tglakhir'])))
			{
				$this->pembelian_model->setTanggalAwal($_POST['tglawal']);
				$this->pembelian_model->setTanggalAkhir($_POST['tglakhir']);
				$dataprofile = array(
					'page_title' => 'Purchasing > Pembelian Barang',
					'name' => $this->session->userdata('fullname'),
					'kodepono' => $this->pembelian_model->getnopoCode(),
					'mastersupp' => $this->mastersupplier_model->getList(),
					'masterbarang' => $this->masterbarangdetail_model->getall(),
					'transpo' => $this->pembelian_model->getlistpertanggal(),
					'links' => $this->pagination_editable->create_links(),
					'message' =>''
				);  
            }
			elseif($_POST['kdview'] == "0")
			{
				$dataprofile = array(
					'page_title' => 'Purchasing > Pembelian Barang',
					'name' => $this->session->userdata('fullname'),
					'kodepono' => $this->pembelian_model->getnopoCode(),
					'mastersupp' => $this->mastersupplier_model->getList(),
					'masterbarang' => $this->masterbarangdetail_model->getall(),
					'transpo' => $this->pembelian_model->getListlimit($config['per_page'],$page),
					'links' => $this->pagination_editable->create_links(),
					'message' =>''		
				);  
            }
			elseif($_POST['kdview'] == "1")
			{
				$this->pembelian_model->setNoPo($_POST['namaview']);
				$dataprofile = array(
					'page_title' => 'Purchasing > Pembelian Barang',
					'name' => $this->session->userdata('fullname'),
					'kodepono' => $this->pembelian_model->getnopoCode(),
					'mastersupp' => $this->mastersupplier_model->getList(),
					'masterbarang' => $this->masterbarangdetail_model->getall(),
					'transpo' => $this->pembelian_model->getListpono(),
					'links' => $this->pagination_editable->create_links(),
					'message' =>''
				);  
            }
			elseif($_POST['kdview'] == "2")
			{
				$this->pembelian_model->setSuppliername($_POST['namaview']);
				$dataprofile = array(
					'page_title' => 'Purchasing > Pembelian Barang',
					'name' => $this->session->userdata('fullname'),
					'kodepono' => $this->pembelian_model->getnopoCode(),
					'mastersupp' => $this->mastersupplier_model->getList(),
					'masterbarang' => $this->masterbarangdetail_model->getall(),
					'transpo' => $this->pembelian_model->getListkodesupp(),
					'links' => $this->pagination_editable->create_links(),
					'message' =>''
				);  
            }
			elseif($_POST['kdview'] == "3")
			{
				$this->pembelian_model->setProductname($_POST['namaview']);
				$dataprofile = array(
					'page_title' => 'Purchasing > Pembelian Barang',
					'name' => $this->session->userdata('fullname'),
					'kodepono' => $this->pembelian_model->getnopoCode(),
					'mastersupp' => $this->mastersupplier_model->getList(),
					'masterbarang' => $this->masterbarangdetail_model->getall(),
					'transpo' => $this->pembelian_model->getListkodebarang(),
					'links' => $this->pagination_editable->create_links(),
					'message' =>''
				);  
            }
        } 
		else 
		{
			$dataprofile = array(
				'page_title' => 'Purchasing > Pembelian Barang',
				'name' => $this->session->userdata('fullname'),
				'kodepono' => $this->pembelian_model->getnopoCode(),
				'mastersupp' => $this->mastersupplier_model->getList(),
				'masterbarang' => $this->masterbarangdetail_model->getall(),
				'transpo' => $this->pembelian_model->getListlimit($config['per_page'],$page),
				'links' => $this->pagination_editable->create_links(),
				'message' =>''		
			);		
		}
		$content = array(
			'content' => 'purchasing/pembelian/inputpembelian'
		);
		$this->template->load('templates/default',$content,$dataprofile);
	}
	
	function tambahpembelian() 
	{
		$config = array();
		$config['per_page'] = 10;
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		$config['curr_class'] = 'active';
		$config['full_tag_open'] = '<ul id="pagination-flickr">';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['next_link'] = 'Next &gt;&gt;';
		$config['prev_link'] = '&lt;&lt; Previous';
		$config['next_tag_open'] = '<li class="next">';
		$config['next_tag_close'] = '</li>';
		$config['prev_tag_open'] = '<li class="previous-off">';
		$config['prev_tag_close'] = '</li>';
		
		$config["uri_segment"] = 3;
		$config["base_url"] = site_url('pembeliancontroller/index');
		$config['total_rows'] = $this->pembelian_model->countGetList();
		
		$this->pagination_editable->initialize($config);
	
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		
		if (isset($_POST['btnSave'])) 
		{	
			if (isset($_POST['keterangan']) && $_POST['keterangan'] !="")
			{				
				$kdnopook 	= $this->pembelian_model->getnopoCode();
				$tglpo =date('Y-m-d', strtotime('now'));
				$tanggal=date('Y-m-d H:i:s', strtotime('now'));
				
				$jumdata = count($this->input->post('kdbarang'));
				$i = 0;
				$kodeBrg = "";
				$hrgBrg = "";
				$qtydet = "";
				$disc = "";
				$amtdet = "";
				$ttlqty="";
				$ttlamount="";

				foreach($this->input->post('kdbarang') as $kdBarang)
				{
					$kdidok		= $this->pembelian_model->getdetailid();
					
					$kodeBrg	= $_POST['kdbarang'][$i];
					$hrgBrg		= $_POST['hrgbarang'][$i];
					$qtydet 	= $_POST['qty'][$i];
					$disc 		= $_POST['distcoun'][$i];
					$amtdet 	= $_POST['jmlhhrg'][$i];
					$ttlqty 	= $ttlqty +$qtydet;
					$ttlamount 	= $ttlamount +$amtdet;
					
					$this->pembelian_model->setNoPo($kdnopook);
					$this->pembelian_model->setIDdetail($kdidok);
					$this->pembelian_model->setKodeDetailBarang($kodeBrg);
					$this->pembelian_model->setHargaBarang(intval($hrgBrg));
					$this->pembelian_model->setQuantity(intval($qtydet));
					$this->pembelian_model->setDiscount($disc);
					$this->pembelian_model->setAmount($amtdet);			
					$this->pembelian_model->insertdetail();
					
					//var_dump($kodeBrg." - ".$hrgBrg);
					$i++;
				}
				
				$this->pembelian_model->setNoPo($kdnopook);
				$this->pembelian_model->setTanggalpo($tglpo);
				$this->pembelian_model->setKodeSupplier($_POST['kdsupp']);
				$this->pembelian_model->setTotalQuantity(intval($ttlqty));
				$this->pembelian_model->setTotalAmount(intval($ttlamount));
				$this->pembelian_model->setKeterangan($_POST['keterangan']);
				$this->pembelian_model->setCreatedBy($this->session->userdata('userid'));		
				$this->pembelian_model->setCreatedOn($tanggal);		
				$this->pembelian_model->insert();
				
				$dataprofile = array(
					'page_title' => 'Purchasing > Pembelian Barang',
					'name' => $this->session->userdata('fullname'),
					'kodepono' => $this->pembelian_model->ambilnoPO(),
					'transpo' => $this->pembelian_model->gettranspono()	
				);		
				$content = array(
					'content' => 'purchasing/pembelian/formpembelian'
				);
				$this->template->load('templates/default',$content,$dataprofile);
			
			} 
			else 
			{
                $dataprofile = array(
					'page_title' => 'Purchasing > Pembelian Barang',
            		'name' => $this->session->userdata('fullname'),
					'message' => '<div style="color:red;font-size:9pt;font-weight:bold;">Lengkapi Dahulu Form Pembelian Barang </div>',
					'kodepono' => $this->pembelian_model->getnopoCode(),
					'mastersupp' => $this->mastersupplier_model->getList(),
					'masterbarang' => $this->masterbarangdetail_model->getall(),
					'transpo' => $this->pembelian_model->getListlimit($config['per_page'],$page),
					'links' => $this->pagination_editable->create_links()
                );
                $content = array(
                    'content' => 'purchasing/pembelian/inputpembelian'
                );
                $this->template->load('templates/default', $content, $dataprofile);
            }
        }
		else 
		{
			$dataprofile = array(
				'page_title' => 'Purchasing > Pembelian Barang',
				'name' => $this->session->userdata('fullname'),
				'kodepono' => $this->pembelian_model->getnopoCode(),
				'mastersupp' => $this->mastersupplier_model->getList(),
				'masterbarang' => $this->masterbarangdetail_model->getall(),
				'transpo' => $this->pembelian_model->getListlimit($config['per_page'],$page),
				'links' => $this->pagination_editable->create_links(),
				'message' =>''		
			);		
			$content = array(
				'content' => 'purchasing/pembelian/inputpembelian'
			);
			$this->template->load('templates/default',$content,$dataprofile);
        }
    }
	
	function ubahpembelian() 
	{
        $idkdNoPo = $this->uri->segment(3);
		$idkddetail = $this->uri->segment(4);
		$this->pembelian_model->setNoPo($idkdNoPo);
        if (isset($_POST['btnSave'])) 
		{
				
			$tanggal=date('Y-m-d H:i:s', strtotime('now'));
			$this->pembelian_model->setNoPo($_POST['kdpo']);
			$this->pembelian_model->setIDdetail($_POST['kdiddetail']);

			$this->pembelian_model->setKodeDetailBarang($_POST['kdbarang']);
			$this->pembelian_model->setHargaBarang($_POST['hrgbarang']);
			$this->pembelian_model->setQuantity($_POST['qty']);
			$this->pembelian_model->setDiscount($_POST['distcoun']);
			$this->pembelian_model->setAmount($_POST['jmlhhrg']);

            $this->pembelian_model->ubahpembeliandet();
			
			$ttlqty 	= $this->pembelian_model->getTotalqty();
			$ttlamount	= $this->pembelian_model->getTotalamt();
			
			$this->pembelian_model->setTotalQuantity($ttlqty);
			$this->pembelian_model->setTotalAmount($ttlamount);
			$this->pembelian_model->setKeterangan($_POST['keterangan']);
			$this->pembelian_model->setLastUpdateBy($this->session->userdata('userid'));
			$this->pembelian_model->setLastUpdateOn($tanggal);
			
			 $this->pembelian_model->ubahpembelian();

            redirect('pembeliancontroller');
        } else {
            $this->pembelian_model->setNoPo($idkdNoPo);
			$this->pembelian_model->setIDdetail($idkddetail);
            $dataprofile = array(
				'page_title' => 'Purchasing > Pembelian Barang',
				'name' => $this->session->userdata('fullname'),
				'mastersupp' => $this->mastersupplier_model->getList(),
				'masterbarang' => $this->masterbarangdetail_model->getall(),
				'transpo' => $this->pembelian_model->getkodepono(),
				'links' => $this->pagination_editable->create_links()		
			);		
			$content = array(
				'content' => 'purchasing/pembelian/editpembelian'
			);
			$this->template->load('templates/default',$content,$dataprofile);
        }
    }

    function deletepembelian() 
	{
        $Kodepono = $this->uri->segment(3);
        $this->pembelian_model->setNoPo($Kodepono);
        $this->pembelian_model->deletepembelian();
        redirect('pembeliancontroller');
    }
}